﻿namespace EggOfPanKu.Main.Controls
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;

    public class AdornerDragDrop : Adorner
    {
        #region Fields

        UIElement elementToShow = null; // the element to show on screen
        Point position; //the position where to draw the element

        #endregion Fields

        #region Constructors

        //constructor to drag and drop
        public AdornerDragDrop(UIElement element, UIElement elementToShow)
            : base(element)
        {
            this.elementToShow = CreateClone(elementToShow);
        }

        #endregion Constructors

        #region Properties

        //return the count of the visuals
        protected override int VisualChildrenCount
        {
            get { return 1; }
        }

        #endregion Properties

        #region Methods

        //moves the visual around
        public override GeneralTransform GetDesiredTransform(GeneralTransform transform)
        {
            GeneralTransformGroup group = new GeneralTransformGroup();
            group.Children.Add(transform);
            group.Children.Add(new TranslateTransform(position.X, position.Y));
            return group;
        }

        //updates the position of the adorner
        public void UpdatePosition(Point point)
        {
            position = point;
            AdornerLayer parentLayer = Parent as AdornerLayer;
            if (parentLayer != null)
                parentLayer.Update(AdornedElement);
        }

        //make sure that the layout system knows of the element
        protected override Size ArrangeOverride(Size finalSize)
        {
            //TODO:  Find current position within element, and create rectangle using that and size of elementToShow
            elementToShow.Arrange(new Rect(elementToShow.DesiredSize));
            return finalSize;
        }

        //return the visual that we want to display
        protected override System.Windows.Media.Visual GetVisualChild(int index)
        {
            return elementToShow;
        }

        //make sure that the layout system knows of the element
        protected override Size MeasureOverride(Size constraint)
        {
            elementToShow.Measure(constraint);
            return constraint;
        }

        //create a clone of the element being dragged
        private static ContentControl CreateClone(UIElement element)
        {
            ContentControl control = new ContentControl();
            ContentControl element1 = element as ContentControl;
            if (element1 != null)
            {
                control.Content = element1.Content;
                control.ContentTemplate = element1.ContentTemplate;
            }
            ContentPresenter element2 = element as ContentPresenter;
            if (element2 != null)
            {
                control.Content = element2.Content;
                control.ContentTemplate = element2.ContentTemplate;
            }

            return control;
        }

        #endregion Methods
    }
}